<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- winwidget.qdoc -->
<head>
  <title>Qt widgets in Win32</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="57" height="67" border="0" /></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a></td>
</tr></table><h1 class="title">Qt widgets in Win32<br /><span class="subtitle"></span>
</h1>
<p>This examples shows how to use the <a href="qwinwidget.html">QWinWidget</a> class to use Qt widgets inside a native Win32 user interface.</p>
<p>The Window procedure for the native Win32 window implements a message handlers for left and right mouse button clicks.</p>
<pre> LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
     switch (message)
     {
     case WM_LBUTTONUP:
         {
             QWinWidget w(hWnd, 0, 0);
             w.showCentered();
             QMessageBox mb(&quot;Qt on Win32 - modal&quot;,
                 &quot;Is this dialog modal?&quot;,
                 QMessageBox::NoIcon,
                 QMessageBox::Yes | QMessageBox::Default,
                 QMessageBox::No  | QMessageBox::Escape,
                 QMessageBox::NoButton, &amp;w);
             int result = mb.exec();
             Q_UNUSED(result);
         }
         break;</pre>
<p>When the left button is clicked a modal message box is opened. The <a href="qwinwidget.html">QWinWidget</a> class is used to provide a bridge between the Win32 window and the <a href="http://qt.nokia.com/doc/4.6/qmessagebox.html">QMessageBox</a>, and ensures that the Win32 window is modally blocked by the message box.</p>
<pre>     case WM_RBUTTONUP:
         {
             QWinWidget *w = new QWinWidget(hWnd, 0, 0);
             w-&gt;showCentered();
             QMessageBox *mb = new QMessageBox(&quot;Qt on Win32 - modeless&quot;,
                 &quot;Is this dialog modal?&quot;,
                 QMessageBox::NoIcon,
                 QMessageBox::Yes | QMessageBox::Default,
                 QMessageBox::No  | QMessageBox::Escape,
                 QMessageBox::NoButton, w);
             mb-&gt;setModal(false);
             mb-&gt;setAttribute(Qt::WA_DeleteOnClose);
             mb-&gt;show();
         }
         break;</pre>
<p>When the right button is clicked a modeless message box is opened. The <a href="qwinwidget.html">QWinWidget</a> class is used again to provide proper placement and stacking of the message box. Note that this time both the <a href="qwinwidget.html">QWinWidget</a> and the <a href="http://qt.nokia.com/doc/4.6/qmessagebox.html">QMessageBox</a> are created on the heap using operator new. Since the <tt>WDestructiveClose</tt> flag is passed to the <a href="http://qt.nokia.com/doc/4.6/qmessagebox.html">QMessageBox</a> constructor it is however not necessary to delete either of those objects.</p>
<pre>     case WM_KEYDOWN:
         if (wParam != VK_TAB)
             return DefWindowProc(hWnd, message, wParam, lParam);

         SetFocus(winId);

         break;

     case WM_SETFOCUS:
         {
             QString str(&quot;Got focus&quot;);
             QWidget *widget = QWidget::find(HWND(wParam));
             if (widget)
                 str += QString(&quot; from %1 (%2)&quot;).arg(widget-&gt;objectName()).arg(widget-&gt;metaObject()-&gt;className());
             str += &quot;\n&quot;;
             OutputDebugStringA(str.toLocal8Bit().data());
         }
         break;

     case WM_KILLFOCUS:
         {
             QString str(&quot;Lost focus&quot;);
             QWidget *widget = QWidget::find(HWND(wParam));
             if (widget)
                 str += QString(&quot; to %1 (%2)&quot;).arg(widget-&gt;objectName()).arg(widget-&gt;metaObject()-&gt;className());
             str += &quot;\n&quot;;

             OutputDebugStringA(str.toLocal8Bit().data());
         }
         break;

     case WM_DESTROY:
         PostQuitMessage(0);
         break;

     default:
         return DefWindowProc(hWnd, message, wParam, lParam);
     }
     return 0;
 }</pre>
<p>When the Win32 window is closed the application is terminated. Unhandled messages are processed by the default window procedure.</p>
<pre> int APIENTRY wWinMain(HINSTANCE hInstance,
                       HINSTANCE <span class="comment">/*hPrevInstance*/</span>,
                       LPTSTR    <span class="comment">/*lpCmdLine*/</span>,
                       int       nCmdShow)
 {
     WNDCLASSEX wcex;

     wcex.cbSize = sizeof(WNDCLASSEX);

     wcex.style          = CS_HREDRAW | CS_VREDRAW;
     wcex.lpfnWndProc    = (WNDPROC)WndProc;
     wcex.cbClsExtra     = 0;
     wcex.cbWndExtra     = 0;
     wcex.hInstance      = hInstance;
     wcex.hIcon          = NULL;
     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
     wcex.lpszMenuName   = NULL;
     wcex.lpszClassName  = L&quot;qtest&quot;;
     wcex.hIconSm        = NULL;

     ATOM windowClass = RegisterClassEx(&amp;wcex);

     HWND hWnd = CreateWindow((TCHAR*)windowClass, L&quot;Windows Migration Framework Example&quot;,
         WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0, 0, hInstance, 0);
     if (!hWnd)
         return FALSE;</pre>
<p>The application's entry point function <tt>wWinMain</tt> registers a window class and creates a window using the CreateWindow API. Note that the UNICODE versions of all Win32 APIs are used.</p>
<pre>     int argc = 0;
     QApplication a(argc, 0);</pre>
<p>Before the Qt based user interface can be created a <a href="http://qt.nokia.com/doc/4.6/qapplication.html">QApplication</a> object must exist. The translation of the command line arguments is omitted for brevity.</p>
<pre>     QWinWidget win(hWnd);
     winId = win.winId();
     QHBoxLayout hbox(&amp;win);
     hbox.setSpacing(5);
     hbox.setMargin(0);
     QPushButton *pb = new QPushButton(&quot;Qt command button&quot;, &amp;win);
     pb-&gt;setObjectName(&quot;pb&quot;);
     hbox.addWidget(pb);
     QLabel *label = new QLabel(&quot;Some label&quot;, &amp;win);
     label-&gt;setObjectName(&quot;label&quot;);
     hbox.addWidget(label);
     QLineEdit *le1 = new QLineEdit(&amp;win);
     le1-&gt;setObjectName(&quot;le1&quot;);
     hbox.addWidget(le1);
     QLineEdit *le2 = new QLineEdit(&amp;win);
     le1-&gt;setObjectName(&quot;le2&quot;);
     hbox.addWidget(le2);
     QLineEdit *le3 = new QLineEdit(&amp;win);
     le1-&gt;setObjectName(&quot;le3&quot;);
     hbox.addWidget(le3);

     win.move(0, 0);</pre>
<p>The <a href="qwinwidget.html">QWinWidget</a> class is once again used as a bridge between the Win32 window and a Qt widget, <a href="http://qt.nokia.com/doc/4.6/qpushbutton.html">QPushButton</a> this time. Since the <a href="qwinwidget.html">QWinWidget</a> is a proper <a href="http://qt.nokia.com/doc/4.6/qwidget.html">QWidget</a> it can be layouted and positioned like any other <a href="http://qt.nokia.com/doc/4.6/qwidget.html">QWidget</a>.</p>
<pre>     win.show();

     ShowWindow(hWnd, nCmdShow);
     UpdateWindow(hWnd);

     return a.exec();
 }</pre>
<p>Finally the Win32 user interface is displayed, and control is passed to the <a href="http://qt.nokia.com/doc/4.6/qapplication.html">QApplication</a> event loop. Since Windows doesn't show child windows recoursively the Qt widget has to be shown explicitly.</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%" align="left">Copyright &copy; 2010 Nokia Corporation and/or its subsidiary(-ies)</td>
<td width="40%" align="center"><a href="http://qt.nokia.com/doc/trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt Solutions</div></td>
</tr></table></div></address></body>
</html>
